//logs.js
var util = require('../../utils/util.js')
var router = require('../../utils/router.js')
var approuter = router.router
Page({
  data: {
    focus: false,
    inputValue: '',
    orderInfo:[],
    orderInfoUrl:'',
    sessionKey:'',
  },

  // 下拉刷新加载数据
  onPullDownRefresh:function(){
    wx.showToast({
      title: '刷新中',
      icon: 'loading',
      duration: 2000
    });
    var that = this
    var sessionKey = that.data.sessionKey || wx.getStorageSync('LoginSessionKey');
    if (sessionKey) {
      var data = {};
      var header = { 'content-type': 'application/json', 'Cookie': 'JSESSIONID=' + sessionKey }
      util.sendRequest(that.data.orderInfoUrl, 'get', data, header)
        .then(function (reponse) {
          // 成功调用
          // 成功调用
          if (reponse.data.Status == 'SUCCESS') {
            that.setData({
              // 设置值
              orderInfo: reponse.data.Data
            })
          }
          console.log(reponse)
        }, function (error) {
          // 失败调用
          console.log(error)
        })
    }
    console.log("loaddata");
    setTimeout(function () {
      wx.showToast({
        title: '加载成功',
        icon: 'success',
        duration: 2000
      })
    }, 3000)
  },

  // 加载
  onLoad:function(){
    console.log("onLoad")
    var that = this
    that.setData({
      orderInfoUrl: approuter.orderInfoUrl
    })
    var sessionKey = wx.getStorageSync('LoginSessionKey');
    if (sessionKey) {
      var data = {};
      var header = { 'content-type': 'application/json', 'Cookie': 'JSESSIONID=' + sessionKey }
      util.sendRequest(that.data.orderInfoUrl, 'get', data, header)
        .then(function (reponse) {
          // 成功调用
          // 成功调用
          if (reponse.data.Status == 'SUCCESS') {
            that.setData({
              // 设置值
              orderInfo: reponse.data.Data
            })
          }
          console.log(reponse)
        }, function (error) {
          // 失败调用
          console.log(error)
        })
    }else{
      wx.login({
        success: function (res) {
          var header = { 'content-type': 'application/x-www-form-urlencoded' }
          wx.request({
            url: approuter.userLogin,
            data: { code: res.code },
            method: 'GET',
            header: header,
            success: res => {
              if (res.data.Status == 'SUCCESS' && res.data.result.Status == 'SUCCESS') {
                wx.setStorageSync('LoginSessionKey', res.data.result.SessionKey)
                that.setData({
                  sessionKey: res.data.result.SessionKey
                })
               that.pullData();
              }
            }
          })
        }
      })
    }
  },

  pullData:function(){
    var sessionKey = this.data.sessionKey
    if (sessionKey) {
      var header = { 'content-type': 'application/json', 'Cookie': 'JSESSIONID=' + sessionKey }
      util.sendRequest(this.data.orderInfoUrl, 'get', data, header)
        .then(function (reponse) {
          // 成功调用
          // 成功调用
          if (reponse.data.Status == 'SUCCESS') {
            that.setData({
              // 设置值
              orderInfo: reponse.data.Data
            })
          }
          console.log(reponse)
        }, function (error) {
          // 失败调用
          console.log(error)
        })
    }
  },

  bindButtonTap: function() {
    this.setData({
      focus: Date.now()
    })
  },
  bindKeyInput: function(e) {
    this.setData({
      inputValue: e.detail.value
    })
  },
  bindReplaceInput: function(e) {
    var value = e.detail.value
    var pos = e.detail.cursor
    if(pos != -1){
      //光标在中间
      var left = e.detail.value.slice(0,pos)
      //计算光标的位置
      pos = left.replace(/11/g,'2').length
    }

    //直接返回对象，可以对输入进行过滤处理，同时可以控制光标的位置
    return {
      value: value.replace(/11/g,'2'),
      cursor: pos
    }

    //或者直接返回字符串,光标在最后边
    //return value.replace(/11/g,'2'),
  },
  bindHideKeyboard: function(e) {
    if (e.detail.value === '123') {
      //收起键盘
      wx.hideKeyboard()
    }
  },
})